﻿<%@ Page Title="" Language="C#" AutoEventWireup="true" CodeBehind="DisplayReviewFile.aspx.cs" Inherits="CodeDemon.Views.Review.DisplayReviewFile" %>

    <% var revFile = (from p in ViewData.Model.DAO.ReviewFiles
                      where p.ReviewFileID == ViewData.Model.CurrentReviewFileID
                      select p).First(); %>

    
    <div class="jqmWindow" id="ex<%= ViewData.Model.CurrentReviewFileID %>">
        <div style=" text-align: center; margin: 100px 0 0 60px ; width:18em;"><img src="/CodeDemon/Content/spinner-big.gif" /><br/><br/><span class="loading">Loading...</span></div>
    </div>
    
    <div class="reviewFileComments">        
        <a href="#" class="addComment commentTrigger<%= ViewData.Model.CurrentReviewFileID %>">Add Comment</a>
        <span id="commentContainer_<%= ViewData.Model.CurrentReviewFileID %>">
        
        </span>
    </div>
    
    <table cellpadding="0" cellspacing="0">
<% 
			StringBuilder HTMLOutput = new StringBuilder("");
			System.IO.StringReader diffReader = new System.IO.StringReader(revFile.UDiffText);
			//TODO: Stop fetching each line's comments with AJAX the first time just read from the database
			string index = diffReader.ReadLine();
			diffReader.ReadLine();  // this is all === signs
			string oldFile = diffReader.ReadLine();
			string newFile = diffReader.ReadLine();

            //TODO: Stop fetching each files's comments with AJAX the first time just read from the database

			Regex splitter = new Regex(Environment.NewLine);
			string[] lines = splitter.Split(diffReader.ReadToEnd());

			int minusLineNumber = 0;
			int plusLineNumber = 0;
            int plusCount = 0;
            int minusCount = 0;
			bool veryFirst = false;
			for (int i = 0; i < lines.Length; i++)
			{
				if (lines[i].StartsWith("@@"))
				{
					Regex exp = new Regex(@"@@ \-(?<MINUS_LINE>(\d)+),(?<MINUS_LINE_LENGTH>(\d)+) \+(?<PLUS_LINE>(\d)+),(?<PLUS_LINE_LENGTH>(\d)+) @@");
					MatchCollection myMatches = exp.Matches(lines[i]);
					minusLineNumber = Int32.Parse(myMatches[0].Groups["MINUS_LINE"].ToString());
					plusLineNumber = Int32.Parse(myMatches[0].Groups["PLUS_LINE"].ToString());
                    minusCount = Int32.Parse(myMatches[0].Groups["MINUS_LINE_LENGTH"].ToString());
                    plusCount = Int32.Parse(myMatches[0].Groups["PLUS_LINE_LENGTH"].ToString());                    
					if (veryFirst == false)
					{
						veryFirst = true;
					}
					else
					{
                        %>
						<tr class='line'><td valign='top' class='linenumber'>&nbsp;</td><td valign='top' class='linenumber'>&nbsp;</td><td valign='top' class='diffcol'><img title='Lines Truncated' src='/CodeDemon/Content/truncateMarker.gif' /></td><td>...</td></tr>
						<%
					}
					continue;
				}
                
                if( lines[i].StartsWith(@"\ No newline at end of file")) {
                    continue;
                }

                %>
                <script type="text/javascript">

                    $(document).ready(function() {
                    $('#ex<%= ViewData.Model.CurrentReviewFileID %>_<%=i %>').hide();
                    $('#ex<%= ViewData.Model.CurrentReviewFileID %>_<%=i %>').jqm({ ajax: '/CodeDemon/Review/NewReviewFileComment?reviewFileId=<%= ViewData.Model.CurrentReviewFileID %>&lineNumber=<%=i %>', trigger: 'a.commentTrigger<%= ViewData.Model.CurrentReviewFileID %>_<%=i%>', overlay: 50, modal: 1, ajaxText: '<div style="display:block;text-align: center; margin: 50px 0 0 280px ; width:18em;"><img src="/images/spinner-big.gif" /><br/><br/><span class="loading">Loading...</span><br/><br/><br/></div>' });
                    });
                    
                </script>
                
                <%
                
				string line = lines[i];
                line = line.Replace( " ", "&nbsp;" );
                line = line.Replace( "\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" );
                line = line.Replace( "<", "&lt;" );
                line = line.Replace( ">", "&gt;" );
                
                %>
                 <div class="jqmWindow" id="ex<%= ViewData.Model.CurrentReviewFileID %>_<%=i %>">
                     <div style=" text-align: center; margin: 100px 0 0 60px ; width:18em;"><img src="/CodeDemon/Content/spinner-big.gif" /><br/><br/><span class="loading">Loading...</span></div>
                  </div>
                  
                
                <%
                
				if (line.StartsWith(" "))
				{
                    %>
					<tr class='line'><td valign='top' class='linenumber'><a href="#" class="commentTrigger<%= ViewData.Model.CurrentReviewFileID %>_<%=i%>"><%= minusLineNumber++ %></a>&nbsp;</td><td valign='top' class='linenumber'><%= plusLineNumber++%>&nbsp;</td><td valign='top' class='diffcol'></td><td><%= line %> 
					
					<span id="commentContainer_<%= ViewData.Model.CurrentReviewFileID %>_<%= i %>">
					<% var fc = from ReviewFileComment in ViewData.Model.DAO.GetReviewCommentsByReviewFileIdAndLine(ViewData.Model.CurrentReviewFileID, i) select ReviewFileComment; %>

                    <% foreach( var c in fc) {
                           
                            %>
                            
                            <div class="reviewComment linereviewComment sev<%= c.SeverityName %>">
                                 <a name="<%= c.ReviewFileCommentID %>"><span class='commentDetails linecommentDetails'><%= c.ReviewUserEmail %> on <%= c.CommentTimestamp %>&nbsp;&nbsp;<img src="/CodeDemon/Content/P<%= c.Priority %>.gif" alt="Priority <%= c.Priority %>" />&nbsp;&nbsp;SEV:<%= c.SeverityName %></span><span>&nbsp;[<a class="perma" href="/CodeDemon/Review/DisplayReviewFileSingle?reviewFileID=<%= c.ReviewFileID %>#<%= c.ReviewFileCommentID %>">#Link</a>]</span>
                                <div class='commentText linecommentText'><%=c.CommentText %></div>
                            </div>
                            
                            <%} %>
                            <span></span>
                    </span>
                    
					</td></tr>
			<%
					continue;
				}
				if (line.StartsWith("-"))
				{
                    line = line.TrimStart( '-' );
                    %>
					<tr class='line deletedline'><td valign='top' class='linenumber deletedlinenumber'><a href="#" class="commentTrigger<%= ViewData.Model.CurrentReviewFileID %>_<%=i%>"><%= minusLineNumber++%></a>&nbsp;</td><td valign='top' class='linenumber deletedlinenumber'></td><td valign='top' class='linenumber deletedlinenumber diffcol'>-</td><td><%= line %> 
					<span id="commentContainer_<%=ViewData.Model.CurrentReviewFileID %>_<%= i %>">
					<% var fc = from ReviewFileComment in ViewData.Model.DAO.GetReviewCommentsByReviewFileIdAndLine(ViewData.Model.CurrentReviewFileID, i) select ReviewFileComment; %>

                    <% foreach( var c in fc) {
                           
                            %>
                            
                            <div class="reviewComment linereviewComment sev<%= c.SeverityName %>">
                                 <a name="<%= c.ReviewFileCommentID %>"><span class='commentDetails linecommentDetails'><%= c.ReviewUserEmail %> on <%= c.CommentTimestamp %>&nbsp;&nbsp;<img src="/CodeDemon/Content/P<%= c.Priority %>.gif" alt="Priority <%= c.Priority %>" />&nbsp;&nbsp;SEV:<%= c.SeverityName %></span><span>&nbsp;[<a class="perma" href="/CodeDemon/Review/DisplayReviewFileSingle?reviewFileID=<%= c.ReviewFileID %>#<%= c.ReviewFileCommentID %>">#Link</a>]</span>
                                <div class='commentText linecommentText'><%=c.CommentText %></div>
                            </div>
                            
                            <%} %>
                            <span></span>
					</span>
					
					</td></tr>
						<%
					continue;
				}
				if (line.StartsWith("+"))
				{
                    line = line.TrimStart( '+' );
                    %>
					<tr  class='line addedline'><td valign='top' class='linenumber addedlinenumber'></td><td valign='top' class='linenumber addedlinenumber'><a href="#" class="commentTrigger<%= ViewData.Model.CurrentReviewFileID %>_<%=i%>"><%= plusLineNumber++%></a>&nbsp;</td><td valign='top' class='linenumber addedlinenumber diffcol'>+</td><td><%= line %> 
					<span id="commentContainer_<%=ViewData.Model.CurrentReviewFileID %>_<%= i %>">
					<% var fc = from ReviewFileComment in ViewData.Model.DAO.GetReviewCommentsByReviewFileIdAndLine(ViewData.Model.CurrentReviewFileID, i) select ReviewFileComment; %>

                    <% foreach( var c in fc) {
                           
                            %>
                            
                            <div class="reviewComment linereviewComment sev<%= c.SeverityName %>">
                                 <a name="<%= c.ReviewFileCommentID %>"><span class='commentDetails linecommentDetails'><%= c.ReviewUserEmail %> on <%= c.CommentTimestamp %>&nbsp;&nbsp;<img src="/CodeDemon/Content/P<%= c.Priority %>.gif" alt="Priority <%= c.Priority %>" />&nbsp;&nbsp;SEV:<%= c.SeverityName %></span><span>&nbsp;[<a class="perma" href="/CodeDemon/Review/DisplayReviewFileSingle?reviewFileID=<%= c.ReviewFileID %>#<%= c.ReviewFileCommentID %>">#Link</a>]</span>
                                <div class='commentText linecommentText'><%=c.CommentText %></div>
                            </div>
                            
                            <%} %>
                            <span></span>
					</span>
					
					</td></tr>
						<%
					continue;
				}
                if (line.StartsWith(String.Empty) && revFile.ChangeAction != "A ")
				{
				    %>	
                    <tr class='line'><td valign='top' class='linenumber'><a href="#" class="commentTrigger<%= ViewData.Model.CurrentReviewFileID %>_<%=i%>"><%=minusLineNumber++ %></a>&nbsp;</td><td valign='top' class='linenumber'><%=plusLineNumber++%>&nbsp;</td><td valign='top' class='diffcol'></td><td><%= line %>
                    <span id="commentContainer_<%=ViewData.Model.CurrentReviewFileID %>_<%= i %>">
					<% var fc = from ReviewFileComment in ViewData.Model.DAO.GetReviewCommentsByReviewFileIdAndLine(ViewData.Model.CurrentReviewFileID, i) select ReviewFileComment; %>

                    <% foreach( var c in fc) {
                           
                            %>
                            
                            <div class="reviewComment linereviewComment sev<%= c.SeverityName %>">
                                 <a name="<%= c.ReviewFileCommentID %>"><span class='commentDetails linecommentDetails'><%= c.ReviewUserEmail %> on <%= c.CommentTimestamp %>&nbsp;&nbsp;<img src="/CodeDemon/Content/P<%= c.Priority %>.gif" alt="Priority <%= c.Priority %>" />&nbsp;&nbsp;SEV:<%= c.SeverityName %></span><span>&nbsp;[<a class="perma" href="/CodeDemon/Review/DisplayReviewFileSingle?reviewFileID=<%= c.ReviewFileID %>#<%= c.ReviewFileCommentID %>">#Link</a>]</span>
                                <div class='commentText linecommentText'><%=c.CommentText %></div>
                            </div>
                            
                            <%} %>
                            <span></span>
					</span>
                    </td></tr>
                    <%
					continue;
				}
			}

        Response.Write(HTMLOutput.ToString());
    %>

    </table>
  

